package com.lzx.adx.report.support.strategy.channel;

import cn.hutool.core.util.URLUtil;
import com.lzx.adx.report.annnotions.ProductType;
import com.lzx.adx.report.constant.ProductChannelConstant;
import com.lzx.adx.report.dto.AccountDTO;
import com.lzx.adx.report.dto.ReportDTO;
import com.lzx.adx.report.util.URLEncoderDecoderTool;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Component;

import java.nio.charset.StandardCharsets;
import java.util.HashMap;
import java.util.Map;

@Component
@Slf4j
@ProductType(ProductChannelConstant.ONEWAY)
public class OneWayReportStrategy extends AbstractReport{
    @Override
    public String getReportFinalUrl(ReportDTO reportDTO, AccountDTO accountDTO) {
        Map<String,Object> params=new HashMap<>();
        params.put("tid",reportDTO.getBizRequestId());
        params.put("os",reportDTO.getOs());
        if(StringUtils.isNotBlank(reportDTO.getOaid()) && !StringUtils.contains(reportDTO.getOaid(), "__")){
            params.put("oaid", reportDTO.getOaid());
        }
        if(StringUtils.isNotBlank(reportDTO.getOaidSum()) && !StringUtils.contains(reportDTO.getOaidSum(), "__")){
            params.put("oaid_md5", reportDTO.getOaidSum());
        }
        if(StringUtils.isNotBlank(reportDTO.getImeiSum()) && !StringUtils.contains(reportDTO.getImeiSum(), "__")){
            params.put("imei_md5", reportDTO.getImeiSum());
        }
        if(StringUtils.isNotBlank(reportDTO.getImei()) && !StringUtils.contains(reportDTO.getImei(), "__")){
            params.put("imei", reportDTO.getImei());
        }
        if(StringUtils.isNotBlank(reportDTO.getHash_android_id()) && !StringUtils.contains(reportDTO.getHash_android_id(), "__")){
            params.put("android_id_md5", reportDTO.getHash_android_id());
        }

        if(StringUtils.isNotBlank(reportDTO.getIdfa()) && !StringUtils.contains(reportDTO.getIdfa(), "__")){
            params.put("idfa", reportDTO.getIdfa());
        }
        if(StringUtils.isNotBlank(reportDTO.getIdfaSum()) && !StringUtils.contains(reportDTO.getIdfaSum(), "__")){
            params.put("idfa_md5", reportDTO.getIdfaSum());
        }
        if(StringUtils.isNotBlank(reportDTO.getCaid()) && !StringUtils.contains(reportDTO.getCaid(), "__")){
            params.put("caid1", reportDTO.getCaid());
        }
        if(StringUtils.isNotBlank(reportDTO.getIp()) && !StringUtils.contains(reportDTO.getIp(), "__")){
            params.put("ip", reportDTO.getIp());
        }
        if(StringUtils.isNotBlank(reportDTO.getUser_agent()) && !StringUtils.contains(reportDTO.getUser_agent(), "__")){
            String ua = URLEncoderDecoderTool.decodeURL(reportDTO.getUser_agent());
            params.put("ua", URLEncoderDecoderTool.encodeURL(ua));
        }
        params.put("event_time", System.currentTimeMillis());
        params.put("adgroup_id",reportDTO.getAdgroupId());
        params.put("campaign_id", reportDTO.getCampaignId());
        params.put("cid", reportDTO.getContentId());

        String allCabackUrl = String.format(accountDTO.getDspCallbackUrl(), reportDTO.getBizRequestId());

        String urlParams = URLUtil.buildQuery(params, StandardCharsets.UTF_8) + "&callback_url=" + URLEncoderDecoderTool.encodeURL(allCabackUrl);

        return accountDTO.getDspReportUrl() + "&" + urlParams;
    }
}
